#ifndef _POP3CLIENTSESSION_HPP_
#define _POP3CLIENTSESSION_HPP_

#include "jhdefs.hpp"
#include "types.hpp"
#include "net/DialogSocket.hpp"
#include "net/MailMessage.hpp"
#include <ostream>
#include <vector>
#include <string>

NET_NAMESPACE_BEGIN

class CPOP3ClientSession
{
public:
	enum
	{
		POP3_PORT = 110
	};

	struct MessageInfo
	{
		int id;
		int size;
	};

	typedef std::vector<MessageInfo> MessageInfoVec;

	explicit CPOP3ClientSession(const CStreamSocket& socket);
	CPOP3ClientSession(const std::string& host, UInt16 port = POP3_PORT);
	virtual ~CPOP3ClientSession();

	void setTimeout(const CTimeSpan& timeout);
	CTimeSpan getTimeout() const;

	void login(const std::string& username, const std::string& password);
	void close();

	int messageCount();
	void listMessages(MessageInfoVec& messages);

	void retrieveMessage(int id, CMailMessage& message);
	void retrieveMessage(int id, CMailMessage& message, CPartHandler& handler);
	void retrieveMessage(int id, std::ostream& ostr);
	void retrieveHeader(int id, CMessageHeader& header);

	void deleteMessage(int id);

	bool sendCommand(const std::string& command, std::string& response);
	bool sendCommand(const std::string& command, const std::string& arg, std::string& response);
	bool sendCommand(const std::string& command, const std::string& arg1, const std::string& arg2, std::string& response);

protected:
	static bool isPositive(const std::string& response);

private:
	CDialogSocket	m_socket;
	bool			m_isOpen;
};

NET_NAMESPACE_END

#endif
